package fr.xebia.monitoring.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import fr.xebia.monitoring.Monitor;
import fr.xebia.monitoring.MonitorFactory;

public class MonitoredStatement implements Statement {

	private Statement statement;
//	private String rootMonitoringKey;
	private String monitoringKey;

	public MonitoredStatement(String key, Statement stmt) {
		super();
		this.statement = stmt;
//		this.rootMonitoringKey = key;
		this.monitoringKey = key+".statement";
	}

	public void addBatch(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".addBatch()");
		try {
			this.statement.addBatch(sql);
		} finally {
			monitor.stop();
		}
	}

	public void cancel() throws SQLException {
		this.statement.cancel();
	}

	public void clearBatch() throws SQLException {
		this.statement.clearBatch();
	}

	public void clearWarnings() throws SQLException {
		this.statement.clearWarnings();
	}

	public void close() throws SQLException {
		this.statement.close();
	}

	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".execute()");
		try {
			return this.statement.execute(sql, autoGeneratedKeys);
		} finally {
			monitor.stop();
		}
	}

	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".execute()");
		try {
			return this.statement.execute(sql, columnIndexes);
		} finally {
			monitor.stop();
		}
	}

	public boolean execute(String sql, String[] columnNames) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".execute()");
		try {
			return this.statement.execute(sql, columnNames);
		} finally {
			monitor.stop();
		}
	}

	public boolean execute(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".execute()");
		try {
			return this.statement.execute(sql);
		} finally {
			monitor.stop();
		}
	}

	public int[] executeBatch() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeBatch()");
		try {
			return this.statement.executeBatch();
		} finally {
			monitor.stop();
		}
	}

	public ResultSet executeQuery(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeQuery()");
		try {
			return this.statement.executeQuery(sql);
		} finally {
			monitor.stop();
		}
	}

	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeUpdate()");
		try {
			return this.statement.executeUpdate(sql, autoGeneratedKeys);
		} finally {
			monitor.stop();
		}
	}

	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeUpdate()");
		try {
			return this.statement.executeUpdate(sql, columnIndexes);
		} finally {
			monitor.stop();
		}
	}

	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeUpdate()");
		try {
			return this.statement.executeUpdate(sql, columnNames);
		} finally {
			monitor.stop();
		}
	}

	public int executeUpdate(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey+".executeUpdate()");
		try {
			return this.statement.executeUpdate(sql);
		} finally {
			monitor.stop();
		}
	}

	public Connection getConnection() throws SQLException {
		return this.statement.getConnection();
	}

	public int getFetchDirection() throws SQLException {
		return this.statement.getFetchDirection();
	}

	public int getFetchSize() throws SQLException {
		return this.statement.getFetchSize();
	}

	public ResultSet getGeneratedKeys() throws SQLException {
		return this.statement.getGeneratedKeys();
	}

	public int getMaxFieldSize() throws SQLException {
		return this.statement.getMaxFieldSize();
	}

	public int getMaxRows() throws SQLException {
		return this.statement.getMaxRows();
	}

	public boolean getMoreResults() throws SQLException {
		return this.statement.getMoreResults();
	}

	public boolean getMoreResults(int current) throws SQLException {
		return this.statement.getMoreResults(current);
	}

	public int getQueryTimeout() throws SQLException {
		return this.statement.getQueryTimeout();
	}

	public ResultSet getResultSet() throws SQLException {
		return this.statement.getResultSet();
	}

	public int getResultSetConcurrency() throws SQLException {
		return this.statement.getResultSetConcurrency();
	}

	public int getResultSetHoldability() throws SQLException {
		return this.statement.getResultSetHoldability();
	}

	public int getResultSetType() throws SQLException {
		return this.statement.getResultSetType();
	}

	public int getUpdateCount() throws SQLException {
		return this.statement.getUpdateCount();
	}

	public SQLWarning getWarnings() throws SQLException {
		return this.statement.getWarnings();
	}

	public void setCursorName(String name) throws SQLException {
		this.statement.setCursorName(name);
	}

	public void setEscapeProcessing(boolean enable) throws SQLException {
		this.statement.setEscapeProcessing(enable);
	}

	public void setFetchDirection(int direction) throws SQLException {
		this.statement.setFetchDirection(direction);
	}

	public void setFetchSize(int rows) throws SQLException {
		this.statement.setFetchSize(rows);
	}

	public void setMaxFieldSize(int max) throws SQLException {
		this.statement.setMaxFieldSize(max);
	}

	public void setMaxRows(int max) throws SQLException {
		this.statement.setMaxRows(max);
	}

	public void setQueryTimeout(int seconds) throws SQLException {
		this.statement.setQueryTimeout(seconds);
	}
	
}
